﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOMS.DAL;
namespace BOMS.WebUI
{
    public partial class sysUserManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                dbind();
            }
        }
        public void dbind()
        {
            DataSet ds = new DataSet();
            ds = SysAdmin.getAllUser();//TODO
            this.GridView_User.DataSource = ds.Tables[0];
            this.GridView_User.DataBind();
        }

        protected void GridView_User_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
                 int rowIndex = Convert.ToInt32(e.CommandArgument);
               if (rowIndex > -1)
               {
                   string userId = this.GridView_User.DataKeys[rowIndex].Value.ToString();
                   if (e.CommandName == "resetPwd")
                   {
                       string pwd = "boms";
                       Boolean IsSuccess = SysAdmin.resetPwd(userId, pwd);
                       Type cstype = this.GetType();
                       if (IsSuccess == true)
                       {
                           ClientScript.RegisterStartupScript(cstype, "key", "<script>alert('初始成功，新密码为:"+pwd+"');</script>");
                       }
                       else
                       {
                           ClientScript.RegisterStartupScript(cstype, "key", "<script>window.alert('初始失败！');</script>");

                       }
                  }
                   else if (e.CommandName == "delUser")
                   {
                       Boolean IsSuccess = SysAdmin.deleteUser(userId);
                       Type cstype = this.GetType();
                       if (IsSuccess == true)
                       {
                           ClientScript.RegisterStartupScript(cstype, "key", "<script>window.alert('删除成功！');window.location.href=window.location.href;</script>");
                       }
                       else
                       {
                           ClientScript.RegisterStartupScript(cstype, "key", "<script>window.alert('删除失败！');</script>");
                       }

                   }
                   else if (e.CommandName == "changeRole")
                   {
                      // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script language=\"javascript\">winAdd('changeRole'," + userId + ");</script>"); 
                       Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "<script language=\"javascript\">zOpen('changeRole'," + userId + ");</script>"); 


                   }
                   
               }
        }

        protected void GridView_User_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判断是不是数据行
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#abcdef'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    Button btdel = e.Row.Cells[5].Controls[0] as Button;
                    btdel.Attributes.Add("onclick", "javascript:if( confirm('确定删除该用户?')){}else{return false;}");
                }
            }
        }
    }
}